package com.btkjgc.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.extend.AccountUserModel;
import com.btkjgc.service.intf.AccountUserGeneralService_Intf;

@Controller
public class AccountUserGeneralController
{
	static Logger logger = Logger.getLogger(AccountUserGeneralController.class);
	
	@Autowired
	private AccountUserGeneralService_Intf accountuserGeneralService;
	
	@RequestMapping(value = "account_user_general", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request) 
	{
		ModelAndView mv = new ModelAndView("account_user_general");
		listrecord(request,mv);
		return mv;
	}
	
	@SuppressWarnings("unchecked")
	public void listrecord(HttpServletRequest request, ModelAndView mv)
	{	
		String tel = request.getParameter("tel");
		String carbrand = request.getParameter("carbrand");
		String type = request.getParameter("Acctype");
		String account = request.getParameter("account");
		String cartype = request.getParameter("cartype");
		String cityid = request.getParameter("cityid");
		String carstatus = request.getParameter("carstatus");
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}
		
		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null)
		{
			pageIndex = Integer.parseInt(page);
		}
		
		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1={"tel","like",tel};
		Object[] arr2={"carbrand","like",carbrand};
		Object[] arr3={"company","=",account};
		Object[] arr4={"cartypeid","=",cartype};
		Object[] arr5={"type","=",type};
		Object[] arr6={"cityid","=",cityid};
		Object[] arr7={"catdel","=",carstatus};
		paramList.add(arr1);
		paramList.add(arr2);
		paramList.add(arr3);
		paramList.add(arr4);
		paramList.add(arr5);
		paramList.add(arr6);
		paramList.add(arr7);
		
		String sql =
				"SELECT * FROM(SELECT\n" +
						" cat.carbrand,\n" +
						" aut.*, cpt.postage_type,\n" +
						" conpt.id AS conid,\n" +
						" conpt.`name` AS conventional,\n" +
						" conpt.operdate AS condate,\n" +
						" ppt.id AS packid,\n" +
						" ppt.`name` AS pack,\n" +
						" ppt.operdate AS packdate,\n" +
						" adm.username AS operator,\n" +
						" ct.`Name` AS city,\n" +
						" cat.cartypeid,\n" +
						" cat.del AS catdel,\n" +
						" (SELECT name from cartype_t WHERE id=cpt.cartype)cartypename,\n" +
						" CASE cat.del\n" +
						" WHEN 0 THEN\n" +
						" '已绑定'\n" +
						" WHEN 1 THEN\n" +
						" '未绑定'\n" +
						" WHEN 2 THEN\n" +
						" '解绑中'\n" +
						" END carstatus\n" +
						"FROM\n" +
						" account_user_t aut\n" +
						"LEFT JOIN car_account_t cat ON cat.accountid = aut.id\n" +
						"LEFT JOIN cartype_postage_t cpt ON cpt.accountid = aut.id\n" +
						"LEFT JOIN admin_user_t adm ON adm.id = aut.operatorid\n" +
						"LEFT JOIN cities_t ct ON ct.id = aut.cityid\n" +
						"LEFT JOIN conventional_postage_t conpt ON cpt.postage_id = conpt.id\n" +
						" AND cpt.postage_type = 1\n" +
						"LEFT JOIN package_postage_t ppt ON cpt.postage_id = ppt.id\n" +
						" AND cpt.postage_type = 2\n" +
						"GROUP BY id)a\n" +
						"WHERE a.del = 0";
		
		sql = accountuserGeneralService.getwhereSQL(sql, paramList, sortfield);
		
		//获取分页查询的数据结果
		totalRecord = accountuserGeneralService.getCount(sql, paramList);
				
		List<AccountUserModel> accountUserModel = accountuserGeneralService.getList(
				AccountUserModel.class, paramList, sql, pageIndex, 15, sortfield);
		
		//设置后台用户界面所需的其他数据
		accountuserGeneralService.setListData(request,mv,pageIndex,totalRecord,accountUserModel);
		
	}
	
	@RequestMapping(value = "add_account_general/{a}", method = RequestMethod.GET)
	public @ResponseBody
	Integer jsonAddAccountUser(HttpServletRequest request,
			@PathVariable("a") int a, AccountUserModel accountUserModel){
		
		//判断用户类型
		if(accountUserModel.getType()==0){//个人
			AccountUserModel privateModel = new AccountUserModel();
			privateModel.setType(0);
			privateModel.setName(accountUserModel.getName());
			privateModel.setTel(accountUserModel.getTel());
			privateModel.setIdentityid(accountUserModel.getIdentityid());
			privateModel.setProvinceid(accountUserModel.getProvinceid());
			privateModel.setCityid(accountUserModel.getCityid());
			privateModel.setDel(0);
			privateModel.setCreatedate(new Date());
			privateModel.setLoginnum(0);
			
			Integer reInt = accountuserGeneralService
					.insertAccountUser(privateModel);
			return reInt;
		}else{//企业
			AccountUserModel companyModel = new AccountUserModel();
			companyModel.setType(1);
			companyModel.setName(accountUserModel.getCname());
			companyModel.setTel(accountUserModel.getCtel());
			companyModel.setBank_num(accountUserModel.getBank_num());
			companyModel.setBank_account(accountUserModel.getBank_account());
			companyModel.setComment(accountUserModel.getComment());
			companyModel.setDel(0);
			companyModel.setCreatedate(new Date());
			companyModel.setLoginnum(0);
			companyModel.setCompany(accountUserModel.getCname());
			Integer reInt = accountuserGeneralService
					.insertAccountUser(companyModel);
			return reInt;
		}
	}
	
}